/*
 * @Author: lmk
 * @Date: 2021-11-24 15:02:28
 * @LastEditTime: 2021-12-04 02:22:05
 * @LastEditors: lmk
 * @Description:
 */
// 上下拉加载刷新  需要配合 mescroll-body 或者mescroll-uni 组件  使用例子请全局搜索mescroll-body 或者mescroll-uni
import {getList} from '@/api/crud.js'
import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
export default{
	mixins:[MescrollMixin],
	data(){
		return {
			dataList:[],
			totalElements:0,
			canReset:false
		}
	},
	onShow(){
		const getStorage = uni.getStorageSync('backStorage')
		if(!getStorage){
			this.canReset && this.downCallback();
			this.canReset = true;
		}else{
			uni.removeStorageSync('backStorage')
		}
	},
	methods:{
		upCallback(page) {
			const form = {
				...this.form,
				page: page.num,
				size: page.size
			};
			if(this.$store.getters.loginForm) {
				const {uid,userType} = this.$store.getters.loginForm;
				if(!form.hasOwnProperty('uid')) form.uid = uid
				if(!form.hasOwnProperty('userType')) form.userType = userType
			}
			getList(form,this.apiType,this.apiModule,this.name).then(res=>{
				const curPageData = res.content;
				// 设置列表数据
				if(page.num == 0) this.dataList = []; //如果是第一页需手动制空列表
				if(!this.secondArr) this.dataList = [...this.dataList,...curPageData]; //追加新数据
				this.totalElements = res.totalElements;
				this.dataListCallBack&&this.dataListCallBack(res) //重组数据
				const len = !this.secondArr ? curPageData.length : curPageData.reduce((num,next)=>num+=next.list.length,0) //根据是否为二维数组的配置来判断是否解析curPageData
				this.mescroll.endBySize(len, res.totalElements)
				this.sort&&this.sort()
			}).catch(res=>{
				console.log(res)
				this.mescroll.endErr()
			})
		}
	}
}